﻿using Microsoft.SharePoint;

namespace DocLibraryReplicator
{
    internal class SPListOperations
    {
        public static string queryConfigList(SPWeb spWeb, string query)
        {
            // Build a query.
            string result = "NotListed";
            SPQuery spQuery = new SPQuery();
            spQuery.Query = string.Concat(
                           "<Where><Eq>",
                              "<FieldRef Name='Source Library Path and Name'/>",
                              "<Value Type='Text'>" + query + "</Value>",
                           "</Eq></Where>");

            spQuery.ViewFields = string.Concat("<FieldRef Name='Source Library Path and Name' />");

            spQuery.ViewFieldsOnly = true; // Fetch only the data that we need.

            // Get data from a list.
            string listUrl = spWeb.ServerRelativeUrl + "lists/Replicator Configuration";
            SPList spList = spWeb.GetList(listUrl);
            //            result = list.GetItems(spQuery);
            string sourceList = spWeb.Url + "/" + query;

            foreach (SPListItem spItem in spList.Items)
            {
                string a = spItem.GetFormattedValue("Source Library Path and Name").ToString();
                if (spItem.GetFormattedValue("Source Library Path and Name").ToString() == sourceList)
                {
                    result = spItem["Target Library Path and Name"].ToString();
                }
            }
            return result;
        }
    }
}